<?php
namespace App\Http\Controllers\Laifei\Talent;

use App\Http\Controllers\Controller;
use App\Models\Common\Common;
use App\Models\Common\RedisTool;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LaifeiTalentAuthController extends Controller
{

    public function index()
    {

        $all = $this->all;

        try{
            // 参数校验
            if(!isset($all['talent_id'])){
                throw new \Exception('缺少参数talent_id');
            }
            if(!isset($all['status'])){
                throw new \Exception('缺少参数status');
            }
            if(($all['status'] === 2 || $all['status'] === 3) && !isset($all['status'])){
                throw new \Exception('缺少参数:status');
            }
            if(!in_array($all['status'],array_keys(Data::$talentStatus))){
                throw new \Exception('参数错误:status');
            }
            if(!isset($all['intelligent'])){
                throw new \Exception('缺少参数intelligent');
            }

            $talent_id = $all['talent_id'];
            $status = $all['status'];
            $remark = $all['remark'] ?? '';
            $intelligent = $all['intelligent'];
        }catch (\Exception $e){
            Log::error(LaifeiTalentAuthController::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1,'操作失败');
        }

        try{
            DB::beginTransaction();
            $user = DB::table('talent')
                ->leftJoin('lottery_users as lu','talent.we_id','=','lu.we_id')
                ->where('talent.talent_id','=',$all['talent_id'])
                ->limit(1)
                ->lockForUpdate()
                ->select('talent.status','lu.openid','talent.account_name','lu.we_id')
                ->first();
            $user = json_decode(json_encode($user), true);

            //处理操作
            DB::table('talent')->where('talent_id','=',$talent_id)->update([
                'status' => $status,
                'verify_time' => date('Y-m-d H:i:s'),
                'verify_user' => $this->userInfo['id'],
                'intelligent'=>$intelligent,
                'remark' => $remark,
            ]);

            switch ($status){
                case Data::DATA_DB_TALENT_STATUS_SUCCESS: // 通过认证
                    DB::table('talent_log')->insert([
                        'talent_id' => $talent_id,
                        'active_type' => Data::DATA_DB_TALENT_ACTION_PASS,
                        'user_id'=>$this->userInfo['id'],
                    ]);
                    if($intelligent){
                        // 修改用户接收探店推送
                        DB::table('lottery_users')->where('we_id','=',$user['we_id'])->update(['notice'=>1]);
                        //发送微信通知
                        $data = [
                            'first'=>'亲爱的用户您好，您的账号已通过认证！',
                            'keyword1'=>$user['account_name'],
                            'keyword2'=>date('Y-m-d H:i:s'),
                            'remark'=>'您的账号已被认证为达人，当前可接霸王餐、点评补发两种订单',
                        ];
                    } else {
                        //发送微信通知
                        $data = [
                            'first'=>'亲爱的用户您好，您的账号已通过认证！',
                            'keyword1'=>$user['account_name'],
                            'keyword2'=>date('Y-m-d H:i:s'),
                            'remark'=>'您的账号已被认证为普通用户，当前只可接霸王餐订单',
                        ];
                    }
                    Common::sendLaifeiMessage($user['openid'],'certified',$data,'/talent/list');
                    break;
                case Data::DATA_DB_TALENT_STATUS_REFUSE:
                    DB::table('talent_log')->insert([
                        'talent_id' => $talent_id,
                        'active_type' => Data::DATA_DB_TALENT_ACTION_REFUSE,
                        'user_id'=>$this->userInfo['id'],
                    ]);
                    // 修改用户接收探店推送
                    DB::table('lottery_users')->where('we_id','=',$user['we_id'])->update(['notice'=>0]);
                    $data = [
                        'first'=>'您好，您的达人认证信息已更新',
                        'keyword1'=>'认证失败',
                        'keyword2'=>$remark,
                        'remark'=>'请点击进入，重新提交认证申请',
                    ];
                    //发送通知
                    Common::sendLaifeiMessage($user['openid'],'certify_fail',$data,'/talent/list');
                    break;
                case Data::DATA_DB_TALENT_STATUS_FREEZE:
                    // 修改用户接收探店推送
                    DB::table('lottery_users')->where('we_id','=',$user['we_id'])->update(['notice'=>0]);
                    DB::table('talent_log')->insert([
                        'talent_id' => $talent_id,
                        'active_type' => Data::DATA_DB_TALENT_ACTION_FREEZE,
                        'user_id'=>$this->userInfo['id'],
                    ]);
                    break;
            }
            RedisTool::refreshUser($user['we_id']);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            Log::error(LaifeiTalentAuthController::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1,'操作失败',['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
        }
        return $this->setReturnJson(0,'达人状态修改成功');

    }

}
